<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

include "database.php";
include "global.php";

function search($input){

    $sql="SELECT  P.id,P.product_id,P.name,P.amount,T.nameunit,T.name Typename,T.ratio,T.id Typeid FROM product_type T,product P WHERE ( P.product_id like '%".$input."%' OR P.name like '%".$input."%' ) AND (T.product_id=P.id  ) AND (P.amount>0) ";
    $query = mysql_query($sql);
    $i=0;
    while($result = mysql_fetch_array($query)){
        if($result){
            $print[$i]['id']=$result['id'];
            $print[$i]['name']=$result['name'];
            $print[$i]['nameunit']=$result['nameunit'];
            if($result['ratio']>0)
                $print[$i]['amount']=ceil($result['amount']/$result['ratio']);
            else
                $print[$i]['amount']=0;
            $print[$i]['product_id']=$result['product_id'];
            $print[$i]['Typename']=$result['Typename'];
            $print[$i]['Typeid']=$result['Typeid'];
        }
        $i++;
    }
//    $s['id']=123;
//    echo json_encode($s);0
    $output['out']=$print;
    echo json_encode($output);
}
function viewOneProduct($id,$Typeid){
   // echo $Typeid;
    $sql="SELECT  P.id,P.name,P.amount,P.product_id,T.asset,T.name Typename,T.buyprice,T.nameunit,T.id Typeid,T.ratio FROM product P,product_type T WHERE P.id = ".$id." AND T.id=".$Typeid;
    
    $query = mysql_query($sql) or die('error');
    $query = mysql_fetch_array($query);
    $output['id'] = $query['id'];
    $output['name'] = $query['name'];
    $output['amount'] = ceil($query['amount']/$query['ratio']);
    $output['oldamount'] = $query['amount'];
    $output['product_id'] = $query['product_id'];
    $output['asset'] = $query['asset'];
    $output['ratio'] = $query['ratio'];
    $output['Typename'] = $query['Typename'];
    $output['nameunit'] = $query['nameunit'];
    $output['buyprice'] = $query['buyprice'];
    $output['Typeid'] = $query['Typeid'];
    $output['Typeamount'] = $query['Typeamount'];

    $k['s']=$output;
    echo json_encode($k);
}
function showAll($sort){
    $sql = "SELECT * FROM receipt ".$sort;
    $query = mysql_query($sql);
   // echo $sql;

    return $query;
}

function addReceipt ($input){
    foreach($input as $i){
        if($i == ""){
            echo "insert error";
            exit();
        }
    }
    check_item_insert($input);
    extract($input);

    $sql="INSERT INTO receipt(receipt_type,customer_name,date,dateString) VALUES(".$receipt_type.",'".$customer_name."','".$date."','".$dateString."')";
    $query = mysql_query($sql);
    echo "\n".$sql;
    if(!$query){
        echo "query receipt error";
        exit();
    }
    $newId=mysql_insert_id();
    insert_transaction(0,$newId,$input);
    
}
function check_item_insert($input){
    extract($input);
    $listProduct[]=null;
    for($i=0;$i<sizeof($product_id);$i++) {
       if($listProduct[$product_id[$i]]>$oldamount[$i]){
                echo "Buy product Error";
                exit();
       }else
            $listProduct[$product_id[$i]] += $amountSQL[$i]*$ratio[$i];
    }

}
function insert_transaction($typeTran,$receiptid,$get){
    extract($get);
    for($i=0;$i<sizeof($product_id);$i++) {
        if(isset($amountSQL[$i]))
            $sql = "INSERT INTO transaction(receipt_id,product_id,amount,asset,buyprice,transaction_type,product_type_id) VALUES(".$receiptid.",'".$product_id[$i]."',".$amountSQL[$i].",".$asset[$i].",".$buyprice[$i].",".$typeTran.",".$type_id[$i].");";
        else
            $sql = "INSERT INTO transaction(receipt_id,product_id,amount,asset,buyprice,transaction_type,product_type_id) VALUES(".$receiptid.",'".$product_id[$i]."',".$amount[$i].",".$asset[$i].",".$buyprice[$i].",".$typeTran.",".$type_id[$i].");";
        $query = mysql_query($sql);

        echo $sql;
        if(!$query){
            echo "query transaction error";
            exit();
        }
        echo $amount[$i]*$ratio[$i];
        if($typeTran==0) //del amount
            $sql="UPDATE product SET amount=amount-".$amount[$i]*$ratio[$i]." WHERE id=".$product_id[$i];
        $query = mysql_query($sql);

        echo $sql;
        if(!$query){
            echo "update transaction error";
            exit();
        };
    }


}


function query_one($id){
    $sql="SELECT * FROM receipt WHERE id=".$id;
    $query = mysql_query($sql);
    $output = mysql_fetch_array($query);
    return $output;
}

function query_one_product($id){
    $sql="SELECT * FROM transaction T WHERE receipt_id=".$id;
    $query = mysql_query($sql);
    
    return $query;

}

function editReceipt($input){

//     foreach($input as $i){
//        if($i == ""){
//            echo "Edit error";
//            exit();
//        }
//    }
    extract($input);
    for($i=0;$i<sizeof($input['product_id']);$i++) {
        $input['amountSQL'][$i]=$input['amount'][$i];
        $input['amount'][$i]= -($input['amount_old'][$i]-$input['amount'][$i]);
        echo $input['amount'][$i]."\n".$input['ratio'][$i];;
    }
    check_item_insert($input);
    deleteReceipt_transaction($id);
    insert_transaction(0,$id,$input);

    $sql="UPDATE receipt SET receipt_type=".$receipt_type.",customer_name='".$customer_name."' WHERE id=".receipt_id_check_out($id);
    $query = mysql_query($sql);
    
    if(!$query){
        echo "query receipt error";
        exit();
    }
    $sql="SELECT

product_id FROM transaction WHERE receipt_id=".$id;
    $query = mysql_query($sql);



//    $sql="UPDATE product SET product_id=".$product_id.",name='".$name."',priceperone=".$priceperone.",amount=".$amount.",unit_name='".$unit_name."' WHERE id=".$id;
//    if(!mysql_query($sql))
//        echo "update error";
}
function deleteReceipt_transaction($receipt_id){
    $sql="DELETE FROM transaction WHERE receipt_id=".$receipt_id;
    if(!mysql_query($sql))
            echo "query error delete transaction";

}
function deleteReceipt($receipt_id){
    $sql="DELETE FROM receipt WHERE id=".$receipt_id;
    if(!mysql_query($sql))
            echo "query error delete receipt";
    deleteReceipt_transaction($receipt_id);
}
?>
